#include "travls.h"

Map::Map()
{
	city = 'a';
	for(int i=0;i<CITY_NUM;i++)
	{
		Coord temp;
		coords[i]=temp;

		//check for duplicate
		for(int j=0;j<CITY_NUM;j++)
		{
			if( i!=j && coords[i]==coords[j])
			{
				i--;
				break;
			}
		}
	}
	closeHeur = closestHeurPath();
}
Map::~Map()
{
	delete [] closeHeur;
}
void Map::print()
{
	char print[PRINT_SIZE_X][PRINT_SIZE_Y];
	for(int i=0;i<PRINT_SIZE_X;i++)
	{
		for(int j=0;j<PRINT_SIZE_Y;j++)
		{
			print[i][j]='.';
		}
	}
	for(int i=0;i<CITY_NUM;i++)
	{
		int xpos = (int)ceil((1.0* coords[i].getX() *PRINT_SIZE_X/MAP_SIZE_X));
		if(xpos<1) xpos=1;
		if(xpos>PRINT_SIZE_X) xpos=PRINT_SIZE_X;

		int ypos = (int)ceil((1.0* coords[i].getY() *PRINT_SIZE_Y/MAP_SIZE_Y));
		if(ypos<1) ypos=1;
		if(ypos>PRINT_SIZE_Y) ypos=PRINT_SIZE_Y;

		//cout << "x: " << coords[i].getX() << " goes to " << xpos <<endl
		//	 << "y: " << coords[i].getY() << " goes to " << ypos <<endl << endl;
		if (print[xpos-1][ypos-1]=='.')
		{
			print[xpos-1][ypos-1]=city++;
		}
		else
		{
			print[xpos-1][ypos-1]='*';
			city++;
		}

	}

	char temp = 'a';
	for(int i=0;i<CITY_NUM;i++)
	{
		cout << "City " << temp++ << ": " << i << " ( " << coords[i].getX() << " , " << coords[i].getY() << " )" << endl;
	}
	for(int j=0;j<PRINT_SIZE_Y;j++)
	{
		for(int i=0;i<PRINT_SIZE_X;i++)
		{
			cout << print[i][j];
		}
		cout << endl;
	}
	cout << endl;
}

int* Map::closestHeurPath()
{
	int* genome = new int[CITY_NUM];
	for(int i=0;i<CITY_NUM;i++)
	{
		genome[i]=0;
	}

	int count=1;
	double total_dist=0;
		
	while(count<CITY_NUM)
	{
		int close_city;
		double close_dist;
		bool found = false;
		for(int i=0;i<CITY_NUM;i++)
		{
			//do not compare to self
			if(i!=(count-1))
			{
				//check if the city is already used
				bool used = false;
				for(int j=0;j<CITY_NUM;j++)
				{
					if (genome[j]==i)
					{
						used = true;
						break;
					}
				}
				if(used) continue;

				//distance to this city
				double dist = coords[i].distance(coords[genome[count-1]]);

				//compare any city after the first
				//temp store if distance is less
				if(found)
				{
					if( dist < close_dist )
					{
						close_dist = dist;
						close_city = i;
					}
				}

				//if this is first city
				else
				{
					close_dist = dist;
					close_city = i;
					found = true;
				}
			}
		}

		//store the next city in the list
		genome[count]=close_city;
		total_dist+=close_dist;
		count++;
	}
	//add distance from last city back to first
	total_dist += coords[genome[CITY_NUM-1]].distance(coords[genome[0]]);

	cout << "Heur close dist: " << total_dist << endl;
	
	ofstream file ( "heur.txt", ios::app );
	double xPrint[CITY_NUM];
	double yPrint[CITY_NUM];
	for(int i=0;i<CITY_NUM;i++)
	{
		xPrint[i] = coords[genome[i]].getX();
		yPrint[i] = coords[genome[i]].getY();
	}

	//print x
	file << "x=[";
	for(int i=0;i<CITY_NUM;i++)
	{
		file << xPrint[i]/MAP_SIZE_X << ",";
	}
	file << xPrint[0]/MAP_SIZE_X << "]" << endl;
	//print y
	file << "y=[";
	for(int i=0;i<CITY_NUM;i++)
	{
		file << yPrint[i]/MAP_SIZE_Y << ",";
	}
	file << yPrint[0]/MAP_SIZE_Y << "]" << endl;

	file << endl;
	cout << endl;
	file.close();

	return genome;
}